import 'package:flutter/material.dart';
import 'package:myapp/Common/Remote/remote.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:myapp/Common/UserAPI/UserAPIResponsitory.dart';

//Mock data

enum QuickRemote { lock, unlock, lights, horn }

class RemoteSet {

  QuickRemote remote;
  String vin = "LBV6P410XLMB49511";

  RemoteSet({this.remote, this.vin});
}

class RemoteActionsWidget extends StatefulWidget {

  List<RemoteSet> remoteSets;

  RemoteActionsWidget({this.remoteSets});

  @override
  _RemoteActionsWidget createState() => _RemoteActionsWidget(remoteSets);
}

class _RemoteActionsWidget extends State<RemoteActionsWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  List<RemoteSet> remoteSets;
  _RemoteActionsWidget(this.remoteSets);

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {

    UserAPIResponsitory userAPIResponsitory = UserAPIResponsitory(scope: UserAPIScope.gateway);
    RemoteResponsitory remoteResponsitory = RemoteResponsitory(userAPIResponsitory: userAPIResponsitory);

    RemoteBloc bloc = RemoteBloc(responsity: remoteResponsitory);
    return BlocListener<RemoteBloc, RemoteState>(
      listener: (context, state){
        print("current remote statet is ${state.toString()}");
      },
      child: BlocBuilder(
        builder: (context, state){
          return Container(
            height: 60.0,
            child: Padding(
                padding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
                child: Row(
                  children: _buildIconButtons(bloc, context),
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                )
            ),
            color: Colors.grey,
          );
        },
        bloc: bloc),
      bloc: bloc,
    );

  }

  //MARK: - Private Methods
  List<Widget> _buildIconButtons(RemoteBloc bloc, BuildContext context){

    List<Widget> actions = [];
  //  actions.add(_buildDivider());
    var service = "";
    var image = AssetImage("");
    RemoteEvent remoteEvent = RemoteFallBack();

    remoteSets.forEach((remoteSet){
      switch (remoteSet.remote) {
        case QuickRemote.horn:
          remoteEvent = RemoteHorn(remoteSet.vin);
          image = AssetImage("assets/ico_horn.png");
          break;

        case QuickRemote.lights:
          remoteEvent = RemoteFlash(remoteSet.vin);
          image = AssetImage('assets/ico_lights.png');
          break;

        case QuickRemote.lock:
          remoteEvent = RemoteLockDoor(remoteSet.vin);
          image = AssetImage('assets/ico_lock.png');
          break;

        case QuickRemote.unlock:
          remoteEvent = RemoteUnLockDoor(remoteSet.vin);
          image = AssetImage('assets/ico_unlock.png');
          break;

      }
      final Widget remoteButton = _buidIconButton(image, (){
        print("is sending remote service....");
        if (bloc.isExecuting()) {
          print("is sending remote service....");
          showRemoteDisableDialog();
          return;
        }
        bloc.dispatch(remoteEvent);
      });
      actions.add(remoteButton);
      //actions.add(_buildDivider());
    });

    return actions;
  }

  Widget _buidIconButton(ImageProvider image, action(),  {Color color = Colors.white}) {
    return IconButton(
      icon: Image(
        height: 32.0,
        width: 32.0,
        fit: BoxFit.contain,
        color: color,
        image: image,
      ),
      onPressed: action,
    );
  }

  Widget _buildDivider() {
    return Container(
      height: 24.0,
      width: 1.0,
      color: Color(0xFFDDDDDD),
    );
  }

  void showRemoteDisableDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("无法执行"),
            titlePadding: EdgeInsets.all(10.0),
            backgroundColor: Colors.white,
            elevation: 5,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(6.0))
            ),
            children: <Widget>[
              ListTile(
                title: Center(child: Text("若当前有正在执行的远程功能,请稍后再试") ,),
              ),
              ListTile(
                title: Center(child: Text("请确认车机端远程功能已开启") ,),
              )
            ],
          );
        }
    );
  }
}
